/*
 * Copyright (c) 2006-2007,2012 Apple Inc. All Rights Reserved.
 */

#include <Security/vmdh.h>
#include <stdlib.h>
#include <unistd.h>

#include "Security_regressions.h"

static uint8_t dh512_p[] = {
    0xb7, 0x15, 0xb9, 0x4d, 0x16, 0xbc, 0x9f, 0xa9,
    0x2f, 0xee, 0x52, 0x28, 0x12, 0x91, 0x81, 0xaa,
    0x16, 0x65, 0x90, 0x99, 0x73, 0xff, 0x2d, 0xae,
    0xeb, 0x5b, 0x11, 0x7f, 0x98, 0x57, 0x54, 0xe2,
    0x85, 0x30, 0x28, 0x58, 0xac, 0x7a, 0x5e, 0x67,
    0x45, 0x01, 0x2c, 0x3f, 0xff, 0xc8, 0x6a, 0x64,
    0x1d, 0x3e, 0x2d, 0xe2, 0x30, 0xb3, 0x7f, 0x64,
    0xca, 0x96, 0xe2, 0x0b, 0x51, 0xab, 0x53, 0xa3,
};

static uint32_t dh512_g = 2;

static uint8_t dh512_r[] = {
    0x00, 0x01, 0x65, 0xf4, 0x48, 0x8c, 0xe7, 0xdc,
    0x75, 0xa5, 0xee, 0x3f, 0x93, 0x64, 0xcd, 0xf1,
    0x81, 0x7b, 0xfb, 0xd9, 0x12, 0x79, 0xa8, 0x2d,
    0xdb, 0x31, 0x72, 0xe5, 0x01, 0xe8, 0xb5, 0x32,
    0x1a, 0xe1, 0x8e, 0x30, 0x9c, 0x67, 0x58, 0xcc,
    0xf9, 0x72, 0x35, 0xc3, 0x66, 0xeb, 0xe4, 0x50,
    0x41, 0x6e, 0xe2, 0x94, 0x97, 0xfb, 0x4b, 0xab,
    0x50, 0x99, 0x2c, 0xaa, 0xf7, 0x6f, 0xa0, 0x51,
    0x55, 0x37,
};

/* Test basic add delete update copy matching stuff. */
static void tests(void)
{
    vmdh_t vmdh;
    ok((vmdh = vmdh_create(dh512_g, dh512_p, sizeof(dh512_p),
        dh512_r, sizeof(dh512_r))), "vmdh_create");
    uint8_t pub_key[512];
    size_t pub_key_len = sizeof(pub_key);
    ok((vmdh_generate_key(vmdh, pub_key, &pub_key_len)),
        "vmdh_generate_key");

    uint8_t pw[] = { 0x31, 0x32, 0x33, 0x34 };
    size_t pw_len = sizeof(pw);

    uint8_t encpw[vmdh_encpw_len(sizeof(pw))];
    size_t encpw_len = sizeof(encpw);

    ok(vmdh_encrypt_password(vmdh, pub_key, pub_key_len, pw, pw_len,
        encpw, &encpw_len), "vmdh_encrypt_password");

    is(encpw_len, (size_t)16, "encrypted pw is 16 bytes");

    vmdh_destroy(vmdh);
}

int vmdh_40(int argc, char *const *argv)
{
	plan_tests(4);

	tests();

	return 0;
}
